import logging
import allure
from params.allParams import URL


class TestHelp:
    @allure.story("发起服务")
    def test_addhelp(self, token_fix):
        allure.dynamic.title('发起服务')  # 动态获取参数生成标题
        apiKey, token, res, token_random, uid = token_fix  # 从fix中获取预置的工具类和token和uid
        with allure.step('发送发起服务请求'):
            url = f"{URL}/api/help/add"  # 构建完整的 URL
            json = {
                "openid": "o6Kmm5Fy9OwYyt4E0EuFr0qQQ_Yg",
                "mu": "尚-1号楼-1108",
                "wx_id": "1350",
                "form_id": "the formId is no longer available in develop or trial version of this mini program",
                "title": f"校园跑腿",
                "des": "day4.9",
                "total_fee": "2"
            }
            headers = {'token': token, 'uid': uid}
            response = apiKey.post(url=url, headers=headers, json=json)
        with allure.step(f"接口返回信息校验接打印"):
            print(f'发起服务接口请求响应信息:{response.text}')
            print(f'响应状态码:{response.status_code}')
            result = response.json()  # 获取响应结果（只调用一次 json()）
            msg = result.get('msg', '未找到msg字段')  # 获取响应消息
            print(f"响应消息：{msg}")
            # 断言响应消息
            assert result.get('msg') == '操作成功', (
                f"响应消息与预期不符\n"
                f"预期消息：{'操作成功'}\n"
                f"实际消息：{result.get('msg')}"
            )
            assert result.get('code') == 1, '响应状态码与预期不符'
            # 数据库断言
            assert apiKey.db_mysql_select(table='helplist', field='id',
                                          value=apiKey.get_text(response.text, 'oid')) != '', '未找到对应服务'
            print('发布服务接口断言成功，测试通过')
        return response
